package run.halo.app.core.extension.content;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import run.halo.app.extension.AbstractExtension;
import run.halo.app.extension.GVK;
import run.halo.app.extension.GroupVersionKind;

/**
 * <p>ProductType extension.</p>
 * <p>产品类型扩展，用于管理产品分类信息</p>
 *
 * @author halo-product-management
 * @since 2.0.0
 */
@Data
@ToString(callSuper = true)
@GVK(group = Constant.GROUP, version = Constant.VERSION, kind = ProductType.KIND,
    plural = "product-types", singular = "product-type")
@EqualsAndHashCode(callSuper = true)
public class ProductType extends AbstractExtension {

    public static final String KIND = "ProductType";

    public static final GroupVersionKind GVK = GroupVersionKind.fromExtension(ProductType.class);

    @Schema(requiredMode = RequiredMode.REQUIRED)
    private ProductTypeSpec spec;

    @Schema
    private ProductTypeStatus status;

    @JsonIgnore
    public ProductTypeStatus getStatusOrDefault() {
        if (this.status == null) {
            this.status = new ProductTypeStatus();
        }
        return status;
    }

    @JsonIgnore
    public boolean isDeleted() {
        return getMetadata().getDeletionTimestamp() != null;
    }

    @Data
    public static class ProductTypeSpec {
        
        @Schema(requiredMode = RequiredMode.REQUIRED, minLength = 1)
        private String displayName;
        
        @Schema(requiredMode = RequiredMode.NOT_REQUIRED)
        private String description;
        
        @Schema(requiredMode = RequiredMode.NOT_REQUIRED)
        private List<String> images;
        
        @Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "0")
        private Integer priority;
    }

    @Data
    public static class ProductTypeStatus {
        
        /**
         * 该产品类型下的产品数量
         */
        private Integer productCount;
        
        /**
         * 观察到的版本号
         */
        private Long observedVersion;
    }
}